from django.shortcuts import render
# Create your views here.
from rest_framework import status
from rest_framework.generics import RetrieveAPIView
from rest_framework.response import Response

from utils.expressage import execute_data_query


class LogisticsAPIView(RetrieveAPIView):
    """查询快递单号"""

    def retrieve(self, request, *args, **kwargs):
        ExpressNumber = request.query_params.get('ExpressNumber')
        ExpressCompany = request.query_params.get('ExpressCompany')
        if ExpressNumber == None and ExpressCompany == None:
            return Response(status=status.HTTP_412_PRECONDITION_FAILED)
        result = execute_data_query(ExpressNumber, ExpressCompany)
        if result.get('result').get('deliverystatus') == '3':
            receive_time = result.get('result').get('list')[0].get('time')
            result['receive_time'] = receive_time
        return Response(result)
